package com.lianzhihui.romeex.ui.account;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.google.zxing.common.StringUtils;
import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.base.AppConfig;
import com.lianzhihui.romeex.bean.user.WalletInfo;
import com.lianzhihui.romeex.database.DataBaseManager;
import com.lianzhihui.romeex.ui.base.BaseActivity;
import com.lianzhihui.romeex.utils.ImagesUtil;
import com.lianzhihui.romeex.utils.NoDoubleClickUtils;
import com.lianzhihui.romeex.utils.QrUtils.QRUtils;
import com.lianzhihui.romeex.utils.StringTool;
import com.lianzhihui.romeex.utils.UserUtils;

import butterknife.BindView;
import butterknife.OnClick;

import static com.lianzhihui.romeex.utils.StringTool.addHead;

/**
 * Description:地址二维码
 * Author: RAMON
 * CreateDate: 2020-11-2
 * UpdateUser:
 * UpdateDate:
 * UpdateRemark:
 * Version:
 */
public class AddressQRCodeActivity extends BaseActivity {
    @BindView(R.id.include_top_tv_title)
    TextView includeTopTvTitle;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.address_qrcode_tv_address)
    TextView addressQrCodeTvAddress;
    @BindView(R.id.address_qrcode_iv_qrcode)
    ImageView addressQrCodeIvQrCode;
    //    @BindView(R.id.receive_qrcode_iv_qrcode_cut)
//    ImageView mIvQrCode;
//    @BindView(R.id.receive_qrcode_lin_main_cut)
//    LinearLayout mLinMainCut;
    @BindView(R.id.address_qrcode_lin_qrcode)
    LinearLayout mLinCut;
//    @BindView(R.id.receive_qrcode_tv_wallet_addr_cut)
//    TextView mTvAddr;
WalletInfo walletInfo;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_address_qrcode;
    }

    @Override
    protected void initView() {

        includeTopTvTitle.setText(getResources().getString(R.string.address_qrcode_title));
        Intent intent = getIntent();
        if (intent != null) {
            String address = intent.getStringExtra(AppConfig.ADDRESS);

            if (!TextUtils.isEmpty(address)) {
                addressQrCodeTvAddress.setText(addHead(address));
                createQrCode(address);
                 walletInfo = UserUtils.getUser(address);
            } else {
                 walletInfo = DataBaseManager.getUser(address);
                if (walletInfo != null) {
                    createQrCode(walletInfo.getAddress());
                }
            }
            String name = walletInfo.getUserName();
            tv_name.setText(name + "的钱包");
        }

    }

    @Override
    protected void initData() {

    }

    private void createQrCode(String address) {
        addressQrCodeTvAddress.setText(addHead(address));
//        mTvAddr.setText(addHead(address));
/*        String addressStr = KeysConstant.QR_CODE_WALLET_OC_FLAG + KeysConstant.QR_CODE_ADDRESS_FLAG + address;
        String encrypt = AESUtil.encrypt(AppConfig.PAY_QR_CODE_KEYWORD, addressStr);
        ImagesUtil.createQrCodeImg(AppConfig.QR_CODE_HEAD + encrypt, addressQrCodeIvQrCode, 220);
        ImagesUtil.createQrCodeImg(AppConfig.QR_CODE_HEAD + encrypt, mIvQrCode, 220);*/

        String addressStr = QRUtils.type2 + address;

        ImagesUtil.createQrCodeImg(addressStr, addressQrCodeIvQrCode, 220);
//        ImagesUtil.createQrCodeImg(address, mIvQrCode, 220);
    }

    @OnClick({R.id.address_qrcode_lin_copy,
            R.id.include_top_iv_back,
            R.id.address_qrcode_tv_copy_address,
            R.id.address_qrcode_tv_save_qrcode,
    })
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.include_top_iv_back:  // 复制地址
                finish();
                break;
            case R.id.address_qrcode_lin_copy:  // 复制地址
            case R.id.address_qrcode_tv_copy_address: // 复制地址
                if (NoDoubleClickUtils.isDoubleClick()) {
                    copyString(addressQrCodeTvAddress.getText().toString().trim());
                }
                break;
            case R.id.address_qrcode_tv_save_qrcode:  // 保存二维码
                if (NoDoubleClickUtils.isDoubleClick()) {
                    savePic();
                }
                break;
        }
    }

    private void savePic() {
        String[] PERMISSIONS = {
                "android.permission.READ_EXTERNAL_STORAGE",
                "android.permission.WRITE_EXTERNAL_STORAGE"};
        //检测是否有写的权限
        int permission = ContextCompat.checkSelfPermission(this,
                "android.permission.WRITE_EXTERNAL_STORAGE");
        if (permission != PackageManager.PERMISSION_GRANTED) {
            // 没有写的权限，去申请写的权限，会弹出对话框
            ActivityCompat.requestPermissions(this, PERMISSIONS, 1);
        } else {
            ImagesUtil.saveQrCode(AddressQRCodeActivity.this, mLinCut);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case 1: {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // 权限被用户同意
                    ImagesUtil.saveQrCode(AddressQRCodeActivity.this, mLinCut);
                } else {
                    // 权限被用户拒绝了
                }
                return;
            }
        }
    }

}
